package com.canaan.business.service.storehouse.impl;

import java.util.*;

import com.canaan.business.common.enums.basic.YNEnum;
import com.canaan.business.domain.storehouse.InventoryAlert;
import com.canaan.business.mapper.storehouse.InventoryAlertMapper;
import com.canaan.business.service.storehouse.IInventoryAlertService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
/**
 * 库存预警Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@Service
public class InventoryAlertServiceImpl implements IInventoryAlertService {
    @Resource
    private InventoryAlertMapper inventoryAlertMapper;

    /**
     * 查询库存预警
     *
     * @param id 库存预警主键
     * @return 库存预警
     */
    @Override
    public InventoryAlert selectInventoryAlertById(Long id) {
        return inventoryAlertMapper.selectInventoryAlertById(id);
    }

    /**
     * 查询库存预警列表
     *
     * @param inventoryAlert 库存预警
     * @return 库存预警
     */
    @Override
    public List<InventoryAlert> selectInventoryAlertList(InventoryAlert inventoryAlert) {
        return inventoryAlertMapper.selectInventoryAlertList(inventoryAlert);
    }

    /**
     * 新增库存预警
     *
     * @param inventoryAlert 库存预警
     * @return 结果
     */
    @Override
    public int insertInventoryAlert(InventoryAlert inventoryAlert) {
        if(StringUtils.isBlank(inventoryAlert.getNo())){
            inventoryAlert.setNo(TableNoUtil.getNo(InventoryAlert.class));
        }
        inventoryAlert.setCreateBy(SecurityUtils.getUsername());
        inventoryAlert.setCreateTime(DateUtils.getNowDate());
        return inventoryAlertMapper.insertInventoryAlert(inventoryAlert);
    }

    /**
     * 批量新增库存预警
     *
     * @param inventoryAlertList 库存预警列表
     * @return 结果
     */
    @Override
    public int batchInsertInventoryAlert(List<InventoryAlert> inventoryAlertList) {
        String username = "admin";
        Date currerTime = DateUtils.getNowDate();
        // 需要查询当天的库存预警信息，防止重复生成预警信息
        InventoryAlert searchTodayEntity = new InventoryAlert();
        searchTodayEntity.setIsSearchToday(YNEnum.Y.getCode());
        List<InventoryAlert> todayAlert = this.selectInventoryAlertList(searchTodayEntity);
        List<InventoryAlert> addList = new ArrayList<>();
        for (InventoryAlert inventoryAlert : inventoryAlertList){
            long count = 0;
            if (!CollectionUtils.isEmpty(todayAlert)){
                 count = todayAlert.stream().filter(e->e.getStorehouseNo().equals(inventoryAlert.getStorehouseNo())&&e.getProductNo().equals(inventoryAlert.getProductNo())).count();
            }
           if (count<1){
               if(StringUtils.isBlank(inventoryAlert.getNo())){
                   inventoryAlert.setNo(TableNoUtil.getNo(InventoryAlert.class));
               }
               inventoryAlert.setCreateBy(username);
               inventoryAlert.setCreateTime(currerTime);
               addList.add(inventoryAlert);
           }

        }
        int insertNum = 0;
        for (int i=0; i<addList.size();) {
            int endIndex = i+500;
            if (endIndex > addList.size()) {
                endIndex = addList.size();
            }
            insertNum = insertNum + inventoryAlertMapper.batchInsertInventoryAlert(addList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存预警
     *
     * @param inventoryAlert 库存预警
     * @return 结果
     */
    @Override
    public int updateInventoryAlert(InventoryAlert inventoryAlert) {
        inventoryAlert.setUpdateBy(SecurityUtils.getUsername());
        inventoryAlert.setUpdateTime(DateUtils.getNowDate());
        return inventoryAlertMapper.updateInventoryAlert(inventoryAlert);
    }

    /**
     * 批量修改库存预警
     *
     * @param inventoryAlertList 库存预警列表
     * @return 结果
     */
    @Override
    public int batchUpdateInventoryAlert(List<InventoryAlert> inventoryAlertList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (InventoryAlert inventoryAlert : inventoryAlertList){
            inventoryAlert.setUpdateBy(username);
            inventoryAlert.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<inventoryAlertList.size();) {
            int endIndex = i+500;
            if (endIndex > inventoryAlertList.size()) {
                endIndex = inventoryAlertList.size();
            }
            updateNum = updateNum + inventoryAlertMapper.batchUpdateInventoryAlert(inventoryAlertList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存预警
     *
     * @param ids 需要删除的库存预警主键集合
     * @return 结果
     */
    @Override
    public int deleteInventoryAlertByIds(Long[] ids) {
        return inventoryAlertMapper.deleteInventoryAlertByIds(ids);
    }

    /**
     * 删除库存预警信息
     *
     * @param id 库存预警主键
     * @return 结果
     */
    @Override
    public int deleteInventoryAlertById(Long id) {
        return inventoryAlertMapper.deleteInventoryAlertById(id);
    }

    /**
     * 查询库存预警
     *
     * @param no 编号
     * @return 库存预警
     */
    @Override
    public InventoryAlert selectInventoryAlertByNo(String no) {
        return inventoryAlertMapper.selectInventoryAlertByNo(no);
    }

    /**
     * 批量删除库存预警
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteInventoryAlertByNos(String[] nos) {
        return inventoryAlertMapper.deleteInventoryAlertByNos(nos);
    }

    /**
     * 删除库存预警信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteInventoryAlertByNo(String no) {
        return inventoryAlertMapper.deleteInventoryAlertByNo(no);
    }
}
